<?php

/**
 * 这是一个表 "vote" 的模型类。
 *
 * 表 'vote' 有以下字段:
 * @property integer $id
 * @property string $title
 * @property string $start_date
 * @property string $end_date
 * @property integer $spacing
 * @property integer $number
 * @property string $s_remark
 * @property string $p_remark
 * @property string $f_remark
 * @property string $a_remark
 *
 * 以下是表的关联关系:
 * @property History[] $histories
 * @property Users[] $users
 */
class Vote extends CActiveRecord
{
	/**
	 * @return string 表名
	 */
	public function tableName()
	{
		return 'vote';
	}

	/**
	 * @return array 表字段验证规则的数组。
	 */
	public function rules()
	{
		return array(
		    array('title,start_date, end_date', 'required'),
			array('spacing, number', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>20),
			array('start_date, end_date, s_remark, p_remark, f_remark, a_remark', 'safe'),
			// 以下规则用于验证查询函数，如果不做查询可注释此行。
			array('id, title, start_date, end_date, spacing, number, s_remark, p_remark, f_remark, a_remark', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array 关联关系。
	 */
	public function relations()
	{
		return array(
			'histories' => array(self::HAS_MANY, 'History', 'vote_id'),
			'users' => array(self::HAS_MANY, 'Users', 'vote_id'),
		);
	}

	/**
	 * @return array 自定义字段显示文本标签 (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '主键',
			'title' => '名称',
			'start_date' => '开始日期',
			'end_date' => '结束日期',
			'spacing' => '投票间隔',
			'number' => '投票次数',
		    'head_image' => '图片',
			's_remark' => '活动介绍',
			'p_remark' => '如何参与',
			'f_remark' => '活动流程',
			'a_remark' => '奖品介绍',
		);
	}

	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('spacing',$this->spacing);
		$criteria->compare('number',$this->number);
		$criteria->compare('s_remark',$this->s_remark,true);
		$criteria->compare('p_remark',$this->p_remark,true);
		$criteria->compare('f_remark',$this->f_remark,true);
		$criteria->compare('a_remark',$this->a_remark,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Vote the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
